﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EvidenceLib.Case;
using EvidenceService.Core;
using NHibernate;

namespace EvidenceService.Case
{
    /// <summary>
    /// 物品记录单
    /// </summary>
    public class EvidenceBillService : BasicService<EvidenceBill>
    {
        public EvidenceBillService(ISession session)
            : base(session)
        {

        }

        public override void SaveOrUpdate(EvidenceBill entity)
        {
            if (entity.Id == 0)
            {
                int billDate = int.Parse(DateTime.Now.ToString("yyMMdd"));
                string serialNo = this.mSession.CreateSQLQuery(string.Format("exec Ev_UpdateBillSerialNo {0},{1}", (int)entity.BillType, billDate)).UniqueResult<string>();
                entity.SerialNo = serialNo;
            }
            base.SaveOrUpdate(entity);
        }

        /// <summary>
        /// 修改物品单据状态
        /// </summary>
        /// <param name="billId"></param>
        public void ModfiyStockState(int billId)
        {
            string hql = "select sum(ev.Qty) as qtyTotal,sum(ev.StockQty) as stockQtyTotal from Evidence as ev where ev.BillId=" + billId;

            object[] or = mSession.CreateQuery(hql).UniqueResult<object[]>();

            double[] r = new double[2];
            r[0] = (double)or[0];
            r[1] = (double)or[1];

            BillStorageStateEnum cs = BillStorageStateEnum.None;

            if (Math.Abs(r[0] - r[1]) <= double.Epsilon)
            {
                cs = BillStorageStateEnum.AllIn;
            }
            else if (Math.Abs(r[1]) < double.Epsilon)
            {
                cs = BillStorageStateEnum.None;
            }
            else if (r[1] > double.Epsilon && r[1] < r[0])
            {
                cs = BillStorageStateEnum.PartIn;
            }

            hql = string.Format( "update EvidenceBill as eb set StockState={0} where eb.Id ={1}",(int)cs,billId);
            
            IQuery query=mSession.CreateQuery(hql);
            query.ExecuteUpdate();

        }
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="billId"></param>
        public void CheckBill(int billId,int orgId,int checkerId)
        {
            string sql = string.Format("update Ev_EvidenceBill  set CheckDeptId={0},CheckerId={1},IsCheck=1,CheckTime='{2}' where Id={3}", orgId, checkerId, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), billId);

            ISQLQuery query= mSession.CreateSQLQuery(sql);
            query.ExecuteUpdate();
        }

        public bool CheckCloseBill(int billId)
        {
            int state = mSession.CreateSQLQuery("select stockstate from Ev_EvidenceBill where id="+billId).UniqueResult<int>();

            if (state == (int)BillStorageStateEnum.AllOut)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public void CloseBill(int billId)
        {
            string sql = "update Ev_EvidenceBill set IsFinish =1,StockState=256 where id=" + billId;

            mSession.CreateSQLQuery(sql).ExecuteUpdate();


        }
    }
}
